import { login } from '../../api/user/login'
import store from '../../store'
const actions = {
    //用户登录
    login({ commit }, userInfo) {
        return new Promise((resolve, reject) => {
            login(userInfo).then(response => {
                const { data } = response
                if (!data.success) {
                    store.commit('removeTokenInfo')
                    reject(data)
                    
                }
                store.commit('setTokenInfo', { token: data.data.token })
                resolve(data) // 执行成功执行的函数，有了它调用该函数的方法才能使用then
            }).catch(error => {
                store.commit('removeTokenInfo')
                reject(error) // 执行失败执行的函数，有了它调用该函数的方法才能使用catch
            })
        })
    },
    //用户退出登录
    loginOut({ commit }) {
        const data = {
            success:true
        }
        return new Promise((resolve, reject) => {
            store.commit('removeTokenInfo')
                resolve(data)

        })
        
    },
}
export default {
    namespaced: true,
    actions
}